package com.person.modules.sys.controller;

import com.person.common.utils.R;
import com.person.modules.sys.entity.SysCompanyEntity;
import com.person.modules.sys.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

/*
@author  Edliu-630
@date    2021/4/0007 - 9:57
文件上传
*/
@Controller
@RequestMapping("/upload")
public class FileController {

    /*=========================================================*/
    @ResponseBody
    @RequestMapping("/uploadImage")
    public R uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        //获取项目classes/static的地址
        String staticPath = ClassUtils.getDefaultClassLoader().getResource("statics").getPath();
        String fileName = file.getOriginalFilename();  //获取文件名

        // 图片存储目录及图片名称
        String url_path = "upload" + File.separator + fileName;
        //图片保存路径
        String savePath = staticPath + File.separator + url_path;
        System.out.println("图片保存地址：" + savePath);
        // 访问路径=静态资源路径+文件目录路径
        String visitPath = "statics/" + url_path;
        System.out.println("图片访问uri：" + visitPath);

        File saveFile = new File(savePath);
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
        try {
            file.transferTo(saveFile);  //将临时存储的文件移动到真实存储路径下
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.ok();
    }

}